<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  confirm-text="完成"
  title="标本外送"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"
)
  el-form
    el-form-item(label="选择机构")
      mt-select(
        v-bind="selectEquipmentProps"
        label="选择设备"
        :onSelect="onChange"
        v-model="outwardOrgId"
        )
</template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import { useNotify, type SelectProps } from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();

const outwardOrgId = ref("");
const outwardOrgName = ref("");

/**
 * 设备选择下拉框属性
 */
const selectEquipmentProps = computed<SelectProps>(() => {
  const props: SelectProps = {
    loadMethod: () => $api.upms.org.getOrgList({ parentId: "-1" }) ?? [],
    separator: ",",
    placeholder: "请选择机构",
    multiple: false,
  };

  return props;
});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { data, request, loadingMap } = useRequest({
  batchGenerateSendInspectionOutward: (params: any) => {
    return $api.lis.specimen
      .batchGenerateSendInspectionOutward(params)
      .then(() => {
        $notify.success("送检成功");
        emit("message-to-parent");
        appmodel.value = false;
      });
  },
});

const updateConfirmModel = () => {
  if (!outwardOrgId.value) {
    $notify.error("请选择机构");
    return;
  }
  request("batchGenerateSendInspectionOutward", {
    specimenList: props.selectionRow.value,
    outwardOrgId: outwardOrgId.value,
    outwardOrgName: outwardOrgName.value,
  });
};

const onChange = (val: any) => {
  outwardOrgId.value = val.value;
  outwardOrgName.value = val.label;
};

watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
    }
  },
  {
    immediate: true,
  }
);
</script>
